import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {ConfigProvider} from "../../providers/config/config";
import {StorageProvider} from "../../providers/storage/storage";
import {HttpServicesProvider} from "../../providers/http-services/http-services";

@IonicPage({
    segment: 'member/my-share',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-my-share',
    templateUrl: 'my-share.html',
})
export class MySharePage {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public storage: StorageProvider,
        public httpService: HttpServicesProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    //判断用户是否登录
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    userInfo = this.config.userInfoHolder;
    ionViewWillEnter() {
        if (!this.validate.checkLogin()) {//登录
            this.navCtrl.push('LoginPage', {'backPage': 'MemberPage'});
        } else {
            this.getData(null);
        }
    }

    page = 1;
    lists = [];
    getData(infiniteScroll) {
        let tokenInfo = this.storage.get('tokenInfo');
        if (tokenInfo) {
            tokenInfo = JSON.parse(tokenInfo);
            let apiUrl = "/forum/my_share/token/" + tokenInfo['token'] + "/page/" + +this.page;
            this.httpService.requestData(apiUrl, response => {

                //全部数据
                let data = response['data'];
                // this.lists = this.lists.concat(data['data']);
                this.lists = this.lists.concat(data);

                this.page++;
                if(infiniteScroll){
                    infiniteScroll.complete();   /*请求完成数据更新页面*/
                    if(data['lists'].length<data['page_size']){   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    }
                }
            });
        }
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }
}
